﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace Constriction.Constrict
{
    public class ConstrictionManager
    {
        public List<ConstrictBlock> constrictBlocksLeft;
        public List<ConstrictBlock> constrictBlocksRight;

        private int numToConstrict;
        private int minConstrictSpeed, maxConstrictSpeed;

        public ConstrictionManager()
        {
            //72 Block Total in this configuration
            constrictBlocksLeft = new List<ConstrictBlock>();
            constrictBlocksRight = new List<ConstrictBlock>();
            FillConstrictBlocks();

            numToConstrict = 5;
            minConstrictSpeed = 50;
            maxConstrictSpeed = 250;
        }

        private void FillConstrictBlocks()
        {
            //Left
            for (var y = 16; y < MainGame.SCREEN_HEIGHT - 16; y += ConstrictBlock.CONSTRICT_BLOCK_SIZE)
            {
                constrictBlocksLeft.Add(new ConstrictBlock(0, y, ConstrictBlockSideEnum.Left));
            }

            //Right
            for (var y = 16; y < MainGame.SCREEN_HEIGHT - 16; y += ConstrictBlock.CONSTRICT_BLOCK_SIZE)
            {
                constrictBlocksRight.Add(new ConstrictBlock(MainGame.SCREEN_WIDTH - MainGame.WALL_SIZE, y, ConstrictBlockSideEnum.Right));
            }
        }

        public void Update(GameTime gameTime)
        {
            int numMoving = 0;

            for (var i = 0; i < constrictBlocksLeft.Count; i++)
            {
                constrictBlocksLeft[i].Update(gameTime);
                constrictBlocksRight[i].Update(gameTime);

                if (constrictBlocksLeft[i].isMoving || constrictBlocksRight[i].isMoving)
                {
                    numMoving++;
                }

                HandleCollision(constrictBlocksLeft[i], constrictBlocksRight[i]);
            }

            if (numMoving < numToConstrict)
            {
                StartMoving(numToConstrict - numMoving);
            }
        }

        private void HandleCollision(ConstrictBlock block1, ConstrictBlock block2)
        {
            if(block1.boundedBox.Intersects(block2.boundedBox)) {
                block1.movingOut = false;
                block2.movingOut = false;
            }
        }

        private void StartMoving(int numToStartConstrict)
        {
            for (var i = 0; i < numToStartConstrict; i++)
            {
                int index = MainGame.rand.Next(constrictBlocksLeft.Count - 1);
                float speed1 = MainGame.rand.Next(minConstrictSpeed, maxConstrictSpeed);
                float speed2 = MainGame.rand.Next(minConstrictSpeed, maxConstrictSpeed);

                MoveBlock(constrictBlocksLeft[index], speed1);
                MoveBlock(constrictBlocksRight[index], speed2);
            }
        }

        private void MoveBlock(ConstrictBlock block, float speed) {
            if (!block.isMoving)
            {
                block.isMoving = true;
                block.movingOut = true;
                block.constrictSpeed = speed;
            }
        }

        public void Draw(SpriteBatch batch)
        {
            for (var i = 0; i < constrictBlocksLeft.Count; i++)
            {
                constrictBlocksLeft[i].Draw(batch);
                constrictBlocksRight[i].Draw(batch);
            }
        }

        public bool DoesContactPlayer(Rectangle box)
        {
            for (var i = 0; i < constrictBlocksLeft.Count; i++)
            {
                if (box.Intersects(constrictBlocksLeft[i].boundedBox) || box.Intersects(constrictBlocksRight[i].boundedBox))
                {
                    return true;
                }
            }

            return false;
        }
    }
}
